$(() => {

    $.ajax({
        type: "get",
        url: "../server/getcar.php",
        dataType: "json",
        success: function (response) {
            rendenUI(response)
            addcount()
            delBtn()
            totalMoney()
        }
    });

    function rendenUI(data) {
        let html = data.map((ele) => {
            return `
            <li class="car-part4-list" data-id=${ele.good_id}>
            <div class="goods-list">
                <input type="checkbox" name="" id="" class="ches">
                <div class="p4-detil">
                    <img src="" alt="">
                    <div>
                        <p>${ele.title}</p>
                        <i></i>
                        <p>支持7天无忧退货</p>
                    </div>

                </div>
                <div class="p4-price">
                    <del></del>
                    <p class="single-price">${ele.price}</p>
                </div>
                <div class="p4-number">
                    <span class="reduce">-</span>
                    <input type="text" value="${ele.num}" class="sum">
                    <span class = "plus">+</span>
                </div>
                <span class="p4-count">￥${ele.count}</span>
                <div class="p4-action">
                    <p class="delBtn">删除</p>
                    <p>移入我的收藏</p>
                </div>
            </div>
        </li>
            `
        }).join("")
        $(".car-part4>ul").html(html)
    }
    console.log(good_id)

    function addcount() {
        // $(".p4-number input").val("1")
        // let count = $(".p4-price p").text()
        // $(".p4-number span").eq(0).click(function () {
        //     let inpval = $(".p4-number input").val()
        //     inpval = inpval * 1 - 1
        //     $(".p4-number input").val(inpval)
        //     $(".p4-count").text(`${count*inpval}`)
        //     if (inpval <= 1) $(".p4-count").text("0")
        //     if ($(".p4-number input").val() * 1 <= 1) {
        //         $(".p4-number input").val("0")
        //     }
        // })
        // $(".p4-number span").eq(1).click(function () {
        //     let inpval = $(".p4-number input").val()
        //     inpval = inpval * 1 + 1
        //     $(".p4-number input").val(inpval)
        //     $(".p4-count").text(`${count*inpval}`)
        // })

        $("body").on("click", ".plus,.reduce", function () {
            /* 更改数量|发送网络请求 */
            let count;
            if (this.className == "plus") {
                count = $(this).prev().val() * 1 + 1;
                $(this).prev().val(count);
            } else {
                count = $(this).next().val() * 1 - 1;
                $(this).next().val(count);
            }

            let price = $(this).parents(".p4-number").siblings(".p4-price").children("p").text();
            $(this).parents(".p4-number").siblings(".p4-count").text(`￥${price * count}`)
            let gid = $(this).parents(".car-part4-list").attr("data-id")
            updateCartData(this.className, gid);
            totalMoney();
        });

        function updateCartData(flag, good_id) {
            $.ajax({
                url: "../server/addcar.php",
                data: {
                    type: "update",
                    flag,
                    good_id
                },
                success: function (response) {}
            });
        }
    }

    function totalMoney() {
        let total_count = 0;
        let total_price = 0;
        $(".car-part4>ul").each((index, ele) => {
            let count = $(ele).find(".p4-number input").val() * 1;
            let price = $(ele).find(".p4-count").text();
            let pricedata = price.split("￥")
            for (let i = 1; i < pricedata.length; i++) {
                total_price += pricedata[i] * 1
            }
            let sum = $(ele).find(".sum")
            for (let j = 0; j < sum.length; j++) {
                total_count += $(ele).find(".sum").eq(j).val() * 1
            }
        });
        $(".em-num").text(total_count);
        $(".em-count").text("￥" + total_price.toFixed(2));
    };

    function delBtn(){
        $("body").on("click", ".delBtn", function () {
            let good_id = $(this).parents(".car-part4-list").attr("data-id")
            $(this).parents(".car-part4-list").remove()
            $.ajax({

                url: "../server/addcar.php",
                data: {
                    type: "del",
                    good_id
                },
                dataType: "json",
                success: function (response) {
                    
                }
            });
        })

        var cheAll = $(".cheAll");
        var ches = $(".car-part4 :checkbox");
        cheAll.on("click", function(){
            ches.prop("checked",$(this).prop("checked"))
        })

        var cheAll_two = $(".cheAll02");
        var ches = $(".car-part4 :checkbox");
        cheAll_two.on("click", function(){
            ches.prop("checked",$(this).prop("checked"))
        })
    }
})